﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Simple_PM.Models;

namespace Simple_PM.Services.Dashboard
{
    public class DashboardService : IDashboardService
    {

        // 1 - New Task
        // 2 - Change Request
        // 3 - Bugs
        // 4 - To Do
        // 5 - Misc

        public DashboardModel GetDashboardModel(string username)
        {
            var model = new DashboardModel();

            using(var context = new SimplePmEntities())
            {
                long userId = context.Users.Where(m => m.UserName == username).FirstOrDefault().UserId;

                var taskCollection = context.DashboardViews.Where(m => m.UserId == userId).ToList();
                model.Tasks = new List<TaskModel>();

                foreach (var task in taskCollection)
                {
                    model.Tasks.Add(new TaskModel { TaskId = task.TaskId, TaskTitle = task.TaskTitle });
                }

                model.NewTaskCount = context.DashboardViews.Where(m => m.CategoryId == 1).Count();
                model.ChangeRequestCount = context.DashboardViews.Where(m => m.CategoryId == 2).Count();
                model.BugsCount = context.DashboardViews.Where(m => m.CategoryId == 3).Count();
                model.TodoCount = context.DashboardViews.Where(m => m.CategoryId == 4).Count();
                model.MiscellaneousCount = context.DashboardViews.Where(m => m.CategoryId == 5).Count();
            }

            return model;
        }

    }

    public interface IDashboardService
    {
        DashboardModel GetDashboardModel(string username);
    }
}